var factoryPaikeOSArr = new Array();
var factoryPaikeOSIDArr = new Array();
var factoryPaikeNameArr = new Array();
var factoryPaikeIDArr = new Array();
var factoryPaikeOSOrderArr = new Array();
var factoryPaikeNameOrderArr = new Array();
var factoryPaikeOSSel;
var factoryPaikeNameSel;
var factoryPaikeTypeSel;
var factoryPaikePeroidSel;

var phoneTypes = new Array();
var phoneTypesWPid  = new Array();
var osTypes = new Array();
var factoryPaikeDates = new Array();
var factoryPaikeData = new Array();

function loadFactoryPaikeList(data) {
	factoryPaikeOSArr = new Array();
	factoryPaikeOSIDArr = new Array();
	factoryPaikeNameArr = new Array();
	factoryPaikeIDArr = new Array();
	factoryPaikeOSOrderArr = new Array();
	factoryPaikeNameOrderArr = new Array();
	factoryPaikeOSSel = document.getElementById("FactoryPaikeOS");
	factoryPaikeNameSel  = document.getElementById("FactoryPaikeFactory");
	factoryPaikeTypeSel  = document.getElementById("FactoryPaikeType");

//	factoryPaikePeroidSel  = document.getElementById("factoryPaikePeroidSel");
	factoryPaikeOSSel.options.length = 0;
	factoryPaikeNameSel.options.length = 0;
	factoryPaikeOSSel.options.add(new  Option("全部", ""));
	factoryPaikeNameSel.options.add(new  Option("全部", ""));

	$(data.items).each(function(i, value){
		var fOS = value["os"];
		var fOSID = value["osid"];
		var fName = value["fname"];
		var fID = value["fid"];
		var ftype = value["type"];
		var fosi = in_array(fOS, factoryPaikeOSArr);
		if(fosi == -1) {
			factoryPaikeOSArr.push(fOS);
			factoryPaikeOSIDArr.push(fOSID);
			fosi=factoryPaikeOSArr.length-1;
			factoryPaikeOSOrderArr[fosi] = new Array();
			factoryPaikeOSOrderArr[fosi][0] = new Array(); //厂商ID
			factoryPaikeOSOrderArr[fosi][1] = new Array(); //厂商名
			factoryPaikeOSSel.options.add(new  Option(fOS, fOSID));
		}
		var fnamei = in_array(fID, factoryPaikeOSOrderArr[fosi][0])
		if (fnamei == -1)
		{
			factoryPaikeOSOrderArr[fosi][0].push(fID);
			factoryPaikeOSOrderArr[fosi][1].push(fName);
		}

		var fnamei = in_array(fName, factoryPaikeNameArr);
		if(fnamei == -1) {
			factoryPaikeNameArr.push(fName);
			factoryPaikeIDArr.push(fID);
			fnamei=factoryPaikeNameArr.length-1;
			factoryPaikeNameOrderArr[fnamei] = new Array();
			factoryPaikeNameOrderArr[fnamei][0] = new Array(); //OSID
			factoryPaikeNameOrderArr[fnamei][1] = new Array(); //OS名
			factoryPaikeNameSel.options.add(new  Option(fName, fID));
		}
		fosi = in_array(fOSID, factoryPaikeNameOrderArr[fnamei][0]);
		if (fosi == -1)
		{
			factoryPaikeNameOrderArr[fnamei][0].push(fOSID);
			factoryPaikeNameOrderArr[fnamei][1].push(fOS);

		}		
	});
	factoryPaikeTypeSel.selectedIndex = 0;
	loadFactoryPaikeDeviceList();
	getFactoryPaikeData();
	jQuery('#body-wrapper').hideLoading();
}


//改变厂商名选项
function changeFactoryPaikeFactory(obj) {
	var factoryPaikeSelOS = factoryPaikeOSSel.options[factoryPaikeOSSel.selectedIndex].text;
	var fnamei = in_array(obj.options[obj.selectedIndex].text, factoryPaikeNameArr);
	var fosi = factoryPaikeOSSel.selectedIndex;

	factoryPaikeOSSel.options.length=0;
	factoryPaikeOSSel.options.add(new  Option("全部", ""));

	//如果厂商选择的不是全部
	if(fnamei != -1) {
		for(i=0; i<factoryPaikeNameOrderArr[fnamei][0].length; i++) {
			factoryPaikeOSSel.options.add(new  Option(factoryPaikeNameOrderArr[fnamei][1][i], factoryPaikeNameOrderArr[fnamei][0][i]));
			if (factoryPaikeSelOS == factoryPaikeNameOrderArr[fnamei][1][i]) 
				factoryPaikeOSSel.selectedIndex = factoryPaikeOSSel.options.length -1;
		}
	}
	//如果厂商选择的是全部
	else {
		for(i=0; i<factoryPaikeOSArr.length; i++) {
			factoryPaikeOSSel.options.add(new  Option(factoryPaikeOSArr[i], factoryPaikeOSIDArr[i]));
			if (factoryPaikeSelOS == factoryPaikeOSArr[i]) 
				factoryPaikeOSSel.selectedIndex = factoryPaikeOSSel.options.length -1;				
			
		}
	}
	loadFactoryPaikeDeviceList();

}

//改变平台名选项
function changeFactoryPaikeOS(obj) {
    
    factoryPaikeNameSel = document.getElementById("FactoryPaikeFactory_cn");
	var factoryPaikeSelFactory = factoryPaikeNameSel.options[factoryPaikeNameSel.selectedIndex].text;
	var fosi = in_array(obj.options[obj.selectedIndex].text, factoryPaikeOSArr);
	var fnamei = factoryPaikeNameSel.selectedIndex;


    factoryPaikeNameSel = document.getElementById("FactoryPaikeFactory");

	factoryPaikeNameSel.options.length=0;
	factoryPaikeNameSel.options.add(new  Option("全部", ""));

	//如果平台选择的不是全部
	if(fosi != -1) {
		for(i=0; i<factoryPaikeOSOrderArr[fosi][0].length; i++) {
			factoryPaikeNameSel.options.add(new  Option(factoryPaikeOSOrderArr[fosi][1][i], factoryPaikeOSOrderArr[fosi][0][i]));
			if (factoryPaikeSelFactory == factoryPaikeOSOrderArr[fosi][1][i])
				factoryPaikeNameSel.selectedIndex = factoryPaikeNameSel.options.length -1;			
		}
	}
	//如果平台选择的是全部
	else {
		for(i=0; i<factoryPaikeNameArr.length; i++) {
			factoryPaikeNameSel.options.add(new  Option(factoryPaikeNameArr[i], factoryPaikeIDArr[i]));
			if (factoryPaikeSelFactory == factoryPaikeNameArr[i]) 
				factoryPaikeNameSel.selectedIndex = factoryPaikeNameSel.options.length -1;
		}
	}
	loadFactoryPaikeDeviceList();

}

//改变终端选项
function changeFactoryPaikeType(obj) {
	loadFactoryPaikeDeviceList();
}

//查询设备列表

function loadFactoryPaikeDeviceList(){
    
    actoryPaikeNameSel = document.getElementById("FactoryPaikeFactory_cn");
    
	if (factoryPaikeOSSel.selectedIndex != 0 && factoryPaikeNameSel.selectedIndex != 0 && factoryPaikeTypeSel.selectedIndex != 0)
	{
		var api = "/client/factoryDeviceMode?btype=paike&fid=" + factoryPaikeNameSel.options[factoryPaikeNameSel.selectedIndex].value +"&osid="+factoryPaikeOSSel.options[factoryPaikeOSSel.selectedIndex].value + "&type=" +factoryPaikeTypeSel.options[factoryPaikeTypeSel.selectedIndex].value;
		$.ajax({
			type: "GET",
			url: api,
			dataType : "jsonp",
			jsonnp: "callback",
			success: readFactoryPaikeDeviceList
		});
	}
	else {
		$("#FactoryPaikeDeviceList").html("<div class=\"ckbText\"><input type=\"checkbox\" id=\"FactoryPaikeDeviceAll\" value=\"All\" checked disabled=\"disabled\">全部机型</div>");
	}

}


//生成设备列表
function readFactoryPaikeDeviceList(data) {
	var html = "";
	if (data.items.length > 0)
	{
		$(data.items).each(function(i, value){
			var pid = value["pid"];
			var device = value["device"];
			html += "<div style=\"float:left\" class=\"ckbText\"><input type=\"checkbox\" name=\"FactoryPaikeDeviceCheckbox\" id=\""+device+"\" value=\""+pid+"\">"+device+"</div>";
		});

	}
	else {
		html = "无对应机型";
	}
	$("#FactoryPaikeDeviceList").html(html);

}

//查询统计数据
var FactoryPaikeDeviceCheckedItems, FactoryPaikeCheckedItems;
function getFactoryPaikeData() {
	var startDate = $("#FactoryPaikeStartDate").val();
	var endDate = $("#FactoryPaikeEndDate").val();
	var osID = document.getElementById("FactoryPaikeOS").options[document.getElementById("FactoryPaikeOS").selectedIndex].value;
	var typeID = document.getElementById("FactoryPaikeType").options[document.getElementById("FactoryPaikeType").selectedIndex].value;
	var fID = document.getElementById("FactoryPaikeFactory_cn").options[document.getElementById("FactoryPaikeFactory_cn").selectedIndex].value;
	$(".FactoryPaikeChart").html("");

	if (startDate != "" && endDate != "")
	{
		var items = document.getElementsByName("FactoryVideoDeviceCheckbox");
		var m = 0;
		for(var i=0;i<items.length;i++) {
			if (items[i].checked) 
				m++;
		}

		if (m > 0 || osID == "" || typeID == "" || fID == "") {
			var start = new Date();
			var end = new Date();

			//统计项
			var items = document.getElementsByName("FactoryPaikeCheckbox");
			FactoryPaikeCheckedItems = new Array();
			for(var i=0;i<items.length;i++) {
				if (items[i].checked)
				{
					FactoryPaikeCheckedItems.push(items[i].value);
				}
			}

			if(FactoryPaikeCheckedItems.length > 0) {
				jQuery('#body-wrapper').showLoading();
				start.setFullYear(startDate.substring(0,4), startDate.substring(5,7)-1, startDate.substring(8,10));
				end.setFullYear(endDate.substring(0,4), endDate.substring(5,7)-1 ,endDate.substring(8,10));

				days = parseInt((end - start)/1000/60/60/24)+1;

				var APIurl = "/client/factoryVideoAnalytics?btype=paike&staDate="+startDate+"&pd="+days;
				if (osID != "")
					APIurl += "&osid="+osID;
				if (typeID != "")
					APIurl += "&type="+typeID;
				if (fID != "")
					APIurl += "&fid="+fID;

				var items = document.getElementsByName("FactoryPaikeDeviceCheckbox");
				FactoryPaikeDeviceCheckedItems = new Array();
				for(var i=0;i<items.length;i++) {
					if (items[i].checked) {
						if(FactoryPaikeDeviceCheckedItems.length == 0)
							APIurl += "&device=";
						else
							APIurl += ",";
						FactoryPaikeDeviceCheckedItems.push(items[i].value);
						APIurl += items[i].value;
					}
				}
				$.ajax({
					type: "GET",
					url: APIurl,
					dataType : "jsonp",
					jsonnp: "callback",
					success: loadFactoryPaikeData
				});
                
                
                $.ajax({
  url: APIurl,
  type: "POST",
  data: "username="+login_username,
success: function(msg){
   //alert('ok');
}
});
                
                
			}
			else
				alert("请选择至少一个统计项");
		}
		else
			alert("请选择至少一个设备型号");
	}
	else {
		alert("请选择时间区域");
	}
}

var FactoryPaikeTabArr, FactoryPaikeDetailArr, FactoryPaikeSumArr, FactoryPaikeDateArr, FactoryPaikeSubTitle;
function loadFactoryPaikeData(data) {
	jQuery('#body-wrapper').hideLoading();	
	var fID = document.getElementById("FactoryPaikeFactory_cn").options[document.getElementById("FactoryPaikeFactory_cn").selectedIndex].value;
	var osID = document.getElementById("FactoryPaikeOS").options[document.getElementById("FactoryPaikeOS").selectedIndex].value;
	var typeID = document.getElementById("FactoryPaikeType").options[document.getElementById("FactoryPaikeType").selectedIndex].value;
	var tab = "";
	var startDate = $("#FactoryPaikeStartDate").val();
	var endDate = $("#FactoryPaikeEndDate").val();
	FactoryPaikeSubTitle = startDate.replace(/\//g, "-") + " 至 " + endDate.replace(/\//g, "-");

	FactoryPaikeTabArr = new Array();
	FactoryPaikeDetailArr = new Array();
	FactoryPaikeSumArr = new Array();
	FactoryPaikeDateArr = new Array();


	//读取数据
	if (data.items.length > 0) //平台选择全部
	{
		if (fID == "") //厂商选择全部
		{	
			changeChartType('FactoryPaike', 'Pie');
			$("#FactoryPaikeLineTab").hide();
			$("#FactoryPaikePieTab").show();
			$("#FactoryPaikeLine").html("");
			//显示选择Tab
			$("#FactoryPaikeTab").html("<div id=\"FactoryPaikeTabAll\" class=\"bitem selbitem\">总览</div>");
			//读取数据
			for(n=0; n<FactoryPaikeCheckedItems.length; n++) 
				FactoryPaikeSumArr[n] = new Array();
			for(i=1; i<document.getElementById("FactoryPaikeFactory_cn").options.length; i++) {
				var tabName = document.getElementById("FactoryPaikeFactory_cn").options[i].text;
				FactoryPaikeTabArr.push(tabName);
				FactoryPaikeDetailArr[i-1] = new Array();
			}
			readSummaryData(data, FactoryPaikeDateArr, "fname", FactoryPaikeTabArr, FactoryPaikeDetailArr, FactoryPaikeCheckedItems, FactoryPaikeSumArr);

			//生成图表
			genTop10Table("FactoryPaikeTable", valueFullNameArr(FactoryPaikeCheckedItems), FactoryPaikeTabArr, FactoryPaikeDetailArr, FactoryPaikeCheckedItems);
			//生成饼图
			genTop10PieChart(FactoryPaikeCheckedItems, "FactoryPaikePie", FactoryPaikeDetailArr, FactoryPaikeSubTitle, "fname", "厂商Top10");
		}
		else if (osID == "") //平台选择全部
		{
			changeChartType('FactoryPaike', 'Pie');
			$("#FactoryPaikeLineTab").hide();
			$("#FactoryPaikePieTab").show();
			$("#FactoryPaikeLine").html("");
			//显示选择Tab
			$("#FactoryPaikeTab").html("<div id=\"FactoryPaikeTabAll\" class=\"bitem selbitem\">总览</div>");
			//读取数据
			for(n=0; n<FactoryPaikeCheckedItems.length; n++) 
				FactoryPaikeSumArr[n] = new Array();
			for(i=1; i<document.getElementById("FactoryPaikeOS").options.length; i++) {
				var tabName = document.getElementById("FactoryPaikeOS").options[i].text;
				FactoryPaikeTabArr.push(tabName);
				FactoryPaikeDetailArr[i-1] = new Array();
			}
			readSummaryData(data, FactoryPaikeDateArr, "os", FactoryPaikeTabArr, FactoryPaikeDetailArr, FactoryPaikeCheckedItems, FactoryPaikeSumArr);
			changeChartType('FactoryPaike', 'Pie');
			//生成图表
			genTop10Table("FactoryPaikeTable", valueFullNameArr(FactoryPaikeCheckedItems), FactoryPaikeTabArr, FactoryPaikeDetailArr, FactoryPaikeCheckedItems);
			//生成饼图
			genTop10PieChart(FactoryPaikeCheckedItems, "FactoryPaikePie", FactoryPaikeDetailArr, FactoryPaikeSubTitle, "os", "平台Top10");

		}
		else if (typeID == "") //终端选择为全部
		{
			changeChartType('FactoryPaike', 'Line');
			$("#FactoryPaikePieTab").show();
			$("#FactoryPaikeTab").show();
			$("#FactoryPaikeLineTab").show();
			//显示选择Tab
			genTabs("FactoryPaike", "FactoryPaikeType", FactoryPaikeTabArr, FactoryPaikeDetailArr, FactoryPaikeSumArr, FactoryPaikeCheckedItems);
			//读取数据
			readData(data, FactoryPaikeDateArr, "type", FactoryPaikeTabArr, FactoryPaikeDetailArr, FactoryPaikeCheckedItems, FactoryPaikeSumArr);
			//生成图表
			genSummaryTable("FactoryPaikeTable", valueFullNameArr(FactoryPaikeCheckedItems), FactoryPaikeDateArr, FactoryPaikeSumArr, -1, "");
			//生成饼图
			genSelPieChart(FactoryPaikeCheckedItems, "FactoryPaikePie", FactoryPaikeTabArr, FactoryPaikeDetailArr, FactoryPaikeSubTitle);
			//生成线形图
			genSummaryLineChart("FactoryPaikeLine", "总览趋势图", FactoryPaikeDateArr, valueFullNameArr(FactoryPaikeCheckedItems), FactoryPaikeSumArr, FactoryPaikeSubTitle);

		}
		else //厂商、平台、终端均不为全部
		{
			changeChartType('FactoryPaike', 'Line');
			$("#FactoryPaikePieTab").hide();
			$("#FactoryPaikePie").html("");
			$("#FactoryPaikeLineTab").show();

			//显示选择Tab
			genDeviceTabs("FactoryPaike", "FactoryPaikeDeviceCheckbox", FactoryPaikeTabArr, FactoryPaikeDetailArr, FactoryPaikeSumArr, FactoryPaikeCheckedItems);
			//读取数据
			readData(data, FactoryPaikeDateArr, "device", FactoryPaikeTabArr, FactoryPaikeDetailArr, FactoryPaikeCheckedItems, FactoryPaikeSumArr);

			//生成图表
			genSummaryTable("FactoryPaikeTable", valueFullNameArr(FactoryPaikeCheckedItems), FactoryPaikeDateArr, FactoryPaikeDetailArr, 0, FactoryPaikeTabArr);
			//生成线形图
			genSummaryLineChart("FactoryPaikeLine", "总览趋势图", FactoryPaikeDateArr, valueFullNameArr(FactoryPaikeCheckedItems), FactoryPaikeDetailArr[0], FactoryPaikeSubTitle);
		}
	}
	else 
		alert("选择的时间区间内无数据");	
}

function changeFactoryPaikeTab(tabName, optionName) {
	if (tabName == "All")
	{
		$("#FactoryPaikePieTab").show();
		$("#FactoryPaikeTab").show();

		//生成饼图
		genSelPieChart(FactoryPaikeCheckedItems, "FactoryPaikePie", FactoryPaikeTabArr, FactoryPaikeDetailArr, MainSubTitle);
		//生成线形图
		genSummaryLineChart("FactoryPaikeLine", "总览趋势图", FactoryPaikeDateArr, valueFullNameArr(FactoryPaikeCheckedItems), FactoryPaikeSumArr, FactoryPaikeSubTitle);
		//生成表格
		genSummaryTable("FactoryPaikeTable", valueFullNameArr(FactoryPaikeCheckedItems), FactoryPaikeDateArr, FactoryPaikeSumArr, -1, "");
	}
	else {
		$("#FactoryPaikePieTab").hide();
		$("#FactoryPaikePie").html("");
		$("#FactoryPaikeLineTab").show();

		//生成线形图
		genSummaryLineChart("FactoryPaikeLine", "总览趋势图", FactoryPaikeDateArr, valueFullNameArr(FactoryPaikeCheckedItems), FactoryPaikeDetailArr[tabName], FactoryPaikeSubTitle);
		//生成表格
		genSummaryTable("FactoryPaikeTable", valueFullNameArr(FactoryPaikeCheckedItems), FactoryPaikeDateArr, FactoryPaikeDetailArr, tabName, FactoryPaikeTabArr);
		if ($("#FactoryPaikePieTab").hasClass("selbitem"))
			changeChartType('FactoryPaike', 'Line');
	} 
	$(".bitem").removeClass("selbitem");
	$("#FactoryPaikeTab"+tabName).addClass("selbitem");		
    getCSVData_save("FactoryPaikeTable");			
}
